//
//  GiftListCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/21.
//

import UIKit

class GiftListCell: UITableViewCell {
    
    lazy var userImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_header"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(18)
        return image
    }()

    lazy var userLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var giftLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var giftImage: UIImageView = {
        return UIImageView()
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.font = .wn_font(name: .regular, size: 12)
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    var model: GiftModel? {
        didSet {
            if let m = model {
                userLabel.text = m.uname
                userImage.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                giftImage.wn_setupImg(m.cover, placeHolder: "gift_send")
                let str = "Gave a \(m.name ?? "")"
                giftLabel.attributedText = String().wn_textChangeColor(str, "\(m.name ?? "")", "",  .init(named: "red_FE95B4")!, .wn_font(name: .bold, size: 14))
                timeLabel.text = m.time?.wn_toJavaDetailDate()
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(userImage)
        contentView.addSubview(userLabel)
        contentView.addSubview(giftLabel)
        contentView.addSubview(giftImage)
        contentView.addSubview(timeLabel)
        contentView.addSubview(lineView)
        userImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.width.height.equalTo(36)
        }
        userLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImage.snp.top).offset(-4)
            make.left.equalTo(userImage.snp.right).offset(12)
        }
        giftLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userLabel.snp.left)
            make.bottom.equalTo(userImage.snp.bottom).offset(4)
        }
        giftImage.snp.makeConstraints { (make) in
            make.centerY.equalTo(giftLabel.snp.centerY)
            make.left.equalTo(giftLabel.snp.right).offset(8)
            make.width.height.equalTo(44)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.right.equalTo(0)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
